﻿using System;
using System.Data;
using System.Collections.Generic;
using Common;
using Model;
using System.Web;
namespace BLL
{
	/// <summary>
	/// User_Volunteer
	/// </summary>
	public partial class User_Volunteer
	{
        #region 登陆
        public string Login(string loginName, string pwd, string validateStr)
        {
            //if (HttpContext.Current.Session["VaildCode"] == null)
            //{
            //    HttpContext.Current.Response.Redirect("~/admin/Login.aspx");
            //}
            //if (HttpContext.Current.Session["VaildCode"].ToString().ToLower() != validateStr.ToLower())
            //{
            //    return "验证码错误！";
            //}
            //用户名为邮箱，大小写统一后匹配。
            loginName = loginName.ToLower();
            //密码MD5
            //pwd = Common.MD5Encrypt.Encrypt(pwd);
            List<Model.User_Volunteer> list = this.GetModelList("LOWER(Email) = '" + loginName + "' and Password = '" + pwd + "'");
            if (list.Count<1)
            {
                return "用户名/密码错误！";
            }
            
            Model.User_Volunteer u = list[0];
            try
            {
                //此用户存在，检查是否可用。不可用时用异常抛出信息。
                CanAction(u);
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            //记录此次登陆。
            RecordLogin(u);
            HttpContext.Current.Session["Volunteer"] = u;
            HttpContext.Current.Response.Redirect("~/admin/Main.aspx");
            return "";
        }

        private void RecordLogin(Model.User_Volunteer u)
        {
            u.LoginCount++;
            u.LastLoginTime = DateTime.Now;
            u.LastIP = HttpContext.Current.Request.UserHostAddress;
            this.Update(u);
        }
        /// <summary>
        /// 检查此账户是否可用
        /// </summary>
        /// <param name="u"></param>
        private void CanAction(Model.User_Volunteer u)
        {
            if (!u.IsEnable)
            {
                throw new Exception("此账号被禁用！");
            }
            if (u.IsDelete)
            {
                throw new Exception("此账号已被删除！");
            }
            if (u.IsFreeze)
            {
                throw new Exception("此账号已被冻结！");
            }
        } 
        #endregion

        /// <summary>
        /// 通过一个志愿者申请
        /// </summary>
        /// <param name="m"></param>
        public bool Apply_Pass(Model.Volunteer_Apply m,ref int id)
        {
            Model.User_Volunteer u = new Model.User_Volunteer();
            u.Age = m.Age;
            u.Area = new Helper_District().Get_FullPath_DistrictId(m.DistrictId, Helper_District.ShowType.斜线连接);
            u.Email = m.Email;
            u.Nickname = m.Name;
            u.PhoneNumber = m.PhoneNumber;
            u.QQ = m.QQ;
            u.RoleId = Get_DefaultRole();
            u.Sex = m.Sex;
            if (DAL.Common.Exist("select * from User_Volunteer where Upper(Email) = '"+u.Email.ToUpper()+"'"))
            {
                return false;
            }
            id =Add(u);
            return true;
        }

        private int Get_DefaultRole()
        {
            return 1;
        }
	}
}

